package com.huadao.cart_ccs.experiment.liquidControl;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.data.task.LiquidLoopTask;
import com.huadao.cart_ccs.data.task.OperateTask;
import com.huadao.cart_ccs.greendao.entity.Operation;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LiquidControlDF extends BaseDialogFragment implements ILiquidControlContract.ILiquidControlView{

    @BindView(R.id.tv_operate_title)
    TextView tv_operate_title;
    @BindView(R.id.operate_amount)
    TextView operate_amount;
    @BindView(R.id.et_operate_amount)
    EditText et_operate_amount;
    @BindView(R.id.tv_operate_date)
    TextView tv_operate_date;
    @BindView(R.id.tv_operate_time)
    TextView tv_operate_time;
    @BindView(R.id.tv_people)
    TextView tv_people;
    @BindView(R.id.btn_operate)
    Button btn_operate;
    @BindView(R.id.btn_stop_operate)
    Button btn_stop_operate;
    @BindView(R.id.tv_history_record)
    TextView tv_history_record;
    @BindView(R.id.et_operate_speed)
    EditText et_operate_speed;
    @BindView(R.id.collect_name)
    TextView collect_name;
    @BindView(R.id.sp_collect_name)
    Spinner sp_collect_name;

    private LiquidControlPresenter presenter;
    @Operation.Operate
    private String operate;
    private LiquidControl liquidControl;
    private Date date;
    private LiquidLoopTask liquidLoopTask;

//    public static final int OPERATE_fluidIntake = 1;
//    public static final int OPERATE_discharge = 2;
//    public static final int OPERATE_change = 3;
//    public static final int OPERATE_collect = 4;

    public static LiquidControlDF newInstance(@Operation.Operate String operate, LiquidLoopTask liquidLoopTask) {
        LiquidControlDF liquidControlDF = new LiquidControlDF();
        Bundle bundle = new Bundle();
        bundle.putString("operate", operate);
        bundle.putSerializable("liquidLoopTask", liquidLoopTask);
        liquidControlDF.setArguments(bundle);
        return liquidControlDF;
    }

    public interface LiquidControl {
        void onLiquidFluidIntake(int amount,int speed, String operator, Date date);

        void onLiquidDiacharge(int amount, int speed,String operator, Date date);

        void onLiquidChange(int amount,int speed, String operator, Date date);

        void onLiquidCollect(int amount,int speed,String collectName, String operator, Date date);

        void onAddCell(int amount,int speed, String operator, Date date);

        void onAddViruses(int amount,int speed, String operator, Date date);

        void onAddCryoprotectant(int amount,int speed, String operator, Date date);

        void onTerminateLiquidFluidIntake();

        void onTerminateLiquidDiacharge();

        void onTerminateLiquidChange();

        void onTerminateLiquidCollect();

        void onTerminateAddCell();

        void onTerminateAddViruses();

        void onTerminateAddCryoprotectant();
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_liquid_control;
    }

    @Override
    protected void onInit() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            operate = bundle.getString("operate");
            liquidLoopTask= (LiquidLoopTask) bundle.get("liquidLoopTask");
        }
        date=new Date();
        tv_operate_date.setText(TimeUtil.date_sdf.format(date));
        tv_operate_time.setText(TimeUtil.time_sdf.format(date));
        tv_people.setText(CartApplication.getInstance().getOperator());
        presenter = new LiquidControlPresenter(this);
        presenter.getOperatesToday();
        initText();

        if(liquidLoopTask!=null&&liquidLoopTask.getState()!= OperateTask.STATE_END){
            btn_stop_operate.setVisibility(View.VISIBLE);
            et_operate_amount.setText(String.valueOf(liquidLoopTask.getTotalAmount()));
            et_operate_amount.setEnabled(false);
            btn_operate.setBackgroundResource(R.drawable.btn_info_bg);
            btn_operate.setText(getOperate()+"中...");
            btn_operate.setEnabled(false);
            tv_operate_date.setText(TimeUtil.date_sdf.format(liquidLoopTask.getOperateDate()));
            tv_operate_time.setText(TimeUtil.time_sdf.format(liquidLoopTask.getOperateDate()));
            tv_people.setText(liquidLoopTask.getOperator());
            String name=liquidLoopTask.getCollectName();
            if(!TextUtils.isEmpty(name)){
                String[] collectName=getResources().getStringArray(R.array.collectName);
                for(int i=0;i<collectName.length;i++){
                    if(collectName[i].equals(name)){
                        sp_collect_name.setSelection(i);
                        break;
                    }
                }
            }
            sp_collect_name.setEnabled(false);
        }else{
            btn_stop_operate.setVisibility(View.GONE);
        }

        this.getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onGetOperatesToday(List<Operation> operationList) {
        tv_history_record.setText("");
        if (operationList == null || operationList.size() == 0) {
            tv_history_record.setVisibility(View.GONE);
            return;
        }
        tv_history_record.setVisibility(View.VISIBLE);
        for (Operation operation : operationList) {
            String text = String.format("%s于%tF %tT进行了%s(%s)操作", operation.getOperator(), operation.getDateTime(), operation.getDateTime(), operation.getName(), operation.getData());
            tv_history_record.append(text);
            tv_history_record.append("\n");
        }
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick({R.id.btn_operate,R.id.btn_close,R.id.btn_stop_operate})
    public void onClick(View view) {
        int id=view.getId();
        if(id==R.id.btn_operate){
            operate();
        }else if(id==R.id.btn_close){
            this.dismiss();
        }else if(id==R.id.btn_stop_operate){
            terminate();
        }

    }

    public void operate() {
        if(liquidControl==null){
            return;
        }
        String err=check();
        if(err!=null){
            DialogFragmentHelper.showMessageDialog(getActivity(),null,err);
            return;
        }
        int amount=Integer.parseInt(et_operate_amount.getText().toString());
        String operator=tv_people.getText().toString();
        int speed= Integer.parseInt(et_operate_speed.getText().toString());
        switch (operate) {
            case Operation.Operate_FluidIntake:
                liquidControl.onLiquidFluidIntake(amount,speed,operator,date);
                break;
            case Operation.Operate_Discharge:
                liquidControl.onLiquidDiacharge(amount,speed,operator,date);
                break;
            case Operation.Operate_Collect:
                String collectName= (String) sp_collect_name.getSelectedItem();
                liquidControl.onLiquidCollect(amount,speed,collectName,operator,date);
                break;
            case Operation.Operate_ChangeLiquid:
                liquidControl.onLiquidChange(amount,speed,operator,date);
                break;
            case Operation.Operate_AddCell:
                liquidControl.onAddCell(amount,speed,operator,date);
                break;
            case Operation.Operate_AddViruses:
                liquidControl.onAddViruses(amount,speed,operator,date);
                break;
            case Operation.Operate_AddCryoprotectant:
                liquidControl.onAddCryoprotectant(amount,speed,operator,date);
                break;
            default:
                break;
        }
        btn_stop_operate.setVisibility(View.VISIBLE);
        et_operate_amount.setEnabled(false);
        btn_operate.setBackgroundResource(R.drawable.btn_info_bg);
        btn_operate.setText(getOperate()+"中...");
        btn_operate.setEnabled(false);
//        this.dismiss();
    }

    private void terminate(){
        switch (operate) {
            case Operation.Operate_FluidIntake:
                liquidControl.onTerminateLiquidFluidIntake();
                break;
            case Operation.Operate_Discharge:
                liquidControl.onTerminateLiquidDiacharge();
                break;
            case Operation.Operate_Collect:
                liquidControl.onTerminateLiquidCollect();
                break;
            case Operation.Operate_ChangeLiquid:
                liquidControl.onTerminateLiquidChange();
                break;
            case Operation.Operate_AddCell:
                liquidControl.onTerminateAddCell();
                break;
            case Operation.Operate_AddViruses:
                liquidControl.onTerminateAddViruses();
                break;
            case Operation.Operate_AddCryoprotectant:
                liquidControl.onTerminateAddCryoprotectant();
                break;
            default:
                break;
        }
        this.dismiss();
    }

    private String check(){
        String err=null;
        if(TextUtils.isEmpty(et_operate_amount.getText())){
            err="请输入体积";
        }else if(TextUtils.isEmpty(tv_people.getText())){
            err="请输入操作人";
        }else if(date==null){
            err="请输入操作时间";
        }else if(TextUtils.isEmpty(et_operate_speed.getText())){
            err="请输入转速";
        }
        return err;
    }

    public void setLiquidControl(LiquidControl liquidControl) {
        this.liquidControl = liquidControl;
    }

//    public void changeOperateBundle(@Operation.Operate String operate){
//        if(this.operate.equals(operate)){
//            return;
//        }else {
//            Bundle bundle = new Bundle();
//            bundle.putString("operate", operate);
//            this.setArguments(bundle);
//        }
//    }

    //根据不同操作设置文字
    private void initText(){
        switch (operate) {
            case Operation.Operate_FluidIntake:
                tv_operate_title.setText(R.string.fluidIntake);
                operate_amount.setText(R.string.fluidIntakeAmount);
                btn_operate.setText(R.string.fluidIntake);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
                break;
            case Operation.Operate_Discharge:
                tv_operate_title.setText(R.string.discharge);
                operate_amount.setText(R.string.dischargeAmount);
                btn_operate.setText(R.string.discharge);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getWastePumpSpeed()));
                break;
            case Operation.Operate_Collect:
                collect_name.setVisibility(View.VISIBLE);
                sp_collect_name.setVisibility(View.VISIBLE);
                tv_operate_title.setText(R.string.collect);
                operate_amount.setText(R.string.collectAmount);
                btn_operate.setText(R.string.collect);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getReclaimPumpSpeed()));
                break;
            case Operation.Operate_ChangeLiquid:
                tv_operate_title.setText(R.string.changeLiquid);
                operate_amount.setText(R.string.changeLiquidAmount);
                btn_operate.setText(R.string.changeLiquid);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
                break;
            case Operation.Operate_AddCell:
                tv_operate_title.setText(R.string.addCell);
                operate_amount.setText(R.string.volume);
                btn_operate.setText(R.string.addCell);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
                break;
            case Operation.Operate_AddViruses:
                tv_operate_title.setText(R.string.addViruses);
                operate_amount.setText(R.string.volume);
                btn_operate.setText(R.string.addViruses);
                et_operate_speed.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
                break;
            default:
                et_operate_speed.setText("50");
                break;
        }
    }

    @Override
    public String getOperate() {
        String name="";
        switch (operate) {
            case Operation.Operate_FluidIntake:
                name=getString(R.string.fluidIntake);
                break;
            case Operation.Operate_Discharge:
                name=getString(R.string.discharge);
                break;
            case Operation.Operate_Collect:
                name=getString(R.string.collect);
                break;
            case Operation.Operate_ChangeLiquid:
                name=getString(R.string.changeLiquid);
                break;
            case Operation.Operate_AddCell:
                name=getString(R.string.addCell);
                break;
            case Operation.Operate_AddViruses:
                name=getString(R.string.addViruses);
                break;
            case Operation.Operate_AddCryoprotectant:
                name=getString(R.string.addCryoprotectant);
                break;
            default:
                break;
        }
        return name;
    }
}
